<template>
  <div>
    <button class="download-button" @click="schoolDownload">试点院校基础数据导出</button><br>
    <button class="download-button" @click="schoolCertApplyDownload">院校证书申报基础数据导出</button><br>
    <button class="download-button" @click="certificateAassessmentDetailsDownload">证书考核详情基础数据导出</button><br>
    <button class="download-button" @click="allianceInfoDownload">联盟信息基础数据导出</button><br>
    <button class="download-button" @click="allianceCertificateInfoDownload">证书归属联盟基础数据导出</button><br>
    <button class="download-button" @click="allDownload">汇总基础数据一键导出</button>
  </div>
</template>

<script>


export default {
  methods: {
    schoolDownload() {
      const filename = '证书申报列表.xlsx';
      const url = `/public/excel/export/${filename}`;
      fetch(url)
          .then((response) => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.blob();
          })
          .then((blob) => {
            handleDownload(blob, filename);
          })
          .catch((error) => {
            console.error('Error downloading file:', error);
          });
    },
    schoolCertApplyDownload() {
      const filename = '证书申报列表.xlsx';
      const url = `/public/excel/export/${filename}`;
      fetch(url)
          .then((response) => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.blob();
          })
          .then((blob) => {
            handleDownload(blob, filename);
          })
          .catch((error) => {
            console.error('Error downloading file:', error);
          });
    },
    certificateAassessmentDetailsDownload() {
      const filename = '证书申报列表.xlsx';
      const url = `/public/excel/export/${filename}`;
      fetch(url)
          .then((response) => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.blob();
          })
          .then((blob) => {
            handleDownload(blob, filename);
          })
          .catch((error) => {
            console.error('Error downloading file:', error);
          });
    },
    allianceInfoDownload() {
      const filename = '证书申报列表.xlsx';
      const url = `/public/excel/export/${filename}`;
      fetch(url)
          .then((response) => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.blob();
          })
          .then((blob) => {
            handleDownload(blob, filename);
          })
          .catch((error) => {
            console.error('Error downloading file:', error);
          });
    },
    allianceCertificateInfoDownload() {
      const filename = '证书申报列表.xlsx';
      const url = `/public/excel/export/${filename}`;
      fetch(url)
          .then((response) => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.blob();
          })
          .then((blob) => {
            handleDownload(blob, filename);
          })
          .catch((error) => {
            console.error('Error downloading file:', error);
          });
    }
    ,
    allDownload() {
      const filename = '222.rar';
      const url = `/public/excel/export/${filename}`;
      fetch(url)
          .then((response) => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.blob();
          })
          .then((blob) => {
            handleDownload(blob, filename);
          })
          .catch((error) => {
            console.error('Error downloading file:', error);
          });
    }
  }


};


// 通用下载方法
const handleDownload = (blob, filename) => {
  if (!(blob instanceof Blob)) {
    console.error('Invalid blob object provided.');
    return;
  }
  console.log('Handling download with blob:', blob); // 添加调试信息
  const link = document.createElement('a');
  link.href = URL.createObjectURL(blob);
  link.download = filename;
  link.click();
  URL.revokeObjectURL(link.href); // 清理对象 URL
};
</script>

<style scoped>.download-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.download-button {
  background-color: #007bff; /* 绿色背景 */
  width: 300px;
  border: none;
  color: white;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 18px;
  margin: 10px 2px;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.download-button:hover {
  background-color: #45a049; /* 悬停时颜色变深 */
}

.download-button:active {
  background-color: #3e8e41; /* 按下时颜色更深 */
}
</style>